package com.yuke.cloud.service.pmc.controller.frontend;

import com.yuke.cloud.common.util.wrapper.WrapMapper;
import com.yuke.cloud.common.util.wrapper.Wrapper;
import com.yuke.cloud.service.pmc.dto.CategoryDto;
import com.yuke.cloud.service.pmc.entity.ProdCategory;
import com.yuke.cloud.service.pmc.service.ProdCategoryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Example;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.MediaType;
import com.yuke.cloud.common.core.support.BaseController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
* Created by zys on 2018/09/28.
*/
@Slf4j
@Api(value = "WEB - ProdCategoryController", tags = "处理ProdCategory相关请求", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@RestController
@RequestMapping("/prod/category")
public class ProdCategoryController extends BaseController {
    @Resource
    private ProdCategoryService prodCategoryService;

    @PostMapping
    @ApiOperation(httpMethod = "POST", value = "新增一条记录")
    public Wrapper add(@ApiParam(name = "prodCategory", value = "该数据记录的POJO") @RequestBody ProdCategory prodCategory) {
        prodCategoryService.save(prodCategory);
        return WrapMapper.ok();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(httpMethod = "DELETE", value = "根据id删除一条记录")
    public Wrapper delete(@ApiParam(name = "id", value = "记录id") @PathVariable Long id) {
//        List<Long> ids  = new ArrayList<>();
//        prodCategoryService.subCategoryId(id, ids);
//        if (ids.size() > 1) {
//            return WrapMapper.ok("该分类下有子分类，请先删除其子分类");
//        }
        ProdCategory prodCategory = prodCategoryService.findById(id);
        Long parentId = prodCategory.getParentId();
        if (!prodCategory.getIsLeaf())
            return WrapMapper.error("该分类下有子分类，请先删除其子分类");
        try {
            prodCategoryService.deleteById(id);

            // 查询其父类下是否还有子类
            ProdCategory temp = new ProdCategory();
            temp.setParentId(parentId);
            int count = prodCategoryService.selectCount(temp);
            if (count <= 0) {
                ProdCategory temp1 = new ProdCategory();
                temp1.setCategoryId(parentId);
                temp1.setIsLeaf(true);
                prodCategoryService.update(temp1);
            }
        } catch (DataIntegrityViolationException e) {
            return WrapMapper.error("数据正在使用，无法删除");
        }
        return WrapMapper.ok();
    }

    @PutMapping
    @ApiOperation(httpMethod = "PUT", value = "修改一条记录")
    public Wrapper update(@ApiParam(name = "prodCategory", value = "需要修改的内容") @RequestBody(required = false) ProdCategory prodCategory) {
        prodCategoryService.update(prodCategory);
        return WrapMapper.ok();
    }

    @GetMapping("/{id}")
    @ApiOperation(httpMethod = "GET", value = "根据ID获取具体信息")
    public Wrapper<CategoryDto> detail(@ApiParam(name = "id", value = "记录id") @PathVariable Long id) {
        CategoryDto categoryDto = prodCategoryService.findInfoById(id);
        return WrapMapper.ok(categoryDto);
    }

    @GetMapping
    @ApiOperation(httpMethod = "GET", value = "分页获取所有记录")
    public Wrapper<List<CategoryDto>> list(@ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                           @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<CategoryDto> list = prodCategoryService.findAllList();
        PageInfo pageInfo = new PageInfo(list);
        return WrapMapper.ok(list);
    }

    //启用禁用时，启用禁用父节点，所有子节点都启用禁用
    @PutMapping("/isEnable/{categoryId}/{isEnable}")
    @ApiOperation(httpMethod = "PUT", value = "修改一条记录")
    public Wrapper<Boolean> isEnable(@ApiParam(name = "categoryId", value = "需要禁用的categoryId") @PathVariable Long categoryId,
                            @ApiParam(name = "isEnable", value = "isEnable") @PathVariable Integer isEnable) {
        int count = prodCategoryService.isEnable(categoryId, isEnable);
        return WrapMapper.ok(count == 0 ? false : true);
    }
    @PostMapping("/ifExist")
    @ApiOperation(httpMethod = "POST", value = "根据ID获取具体信息")
    public Wrapper ifExist(@ApiParam(name = "id", value = "记录id") @RequestBody ProdCategory prodCategory) {
        int count = prodCategoryService.selectCount(prodCategory);
        return WrapMapper.ok(count);
    }


}
